
const router = require('koa-router')();
const mysqlApi = require('../lib/mysql')
const common = require('../lib/common')
const md5 = require('md5')

//登陆页面
router.get('/signin', async (ctx, next) => {
  await ctx.render('signin.ejs', {
    session: ctx.session,
  })
})

//点击登陆
router.post('/signin', async (ctx, next) => {
  let data = ctx.request.body
  await mysqlApi.seletAdmin_tableUsername(data.username).then(win => {
    if (win.length == 1) {
      if (md5(data.password + common.MD5_SUFFIX) == win[0].password) {
        ctx.session.sessionUser = win[0].username
        ctx.session.id = win[0].ID
        ctx.body = {
          code: 200,
          msg: '登陆成功'
        }
      } else {
        ctx.body = {
          code: 400,
          msg: '密码不正确'
        }
      }
    } else {
      ctx.body = {
        msg: '用户名不对',
        code: 4
      }
    }
  })
})

module.exports = router